<?php

/**
 * @author Tu Phan
 * @copyright 2012
 */

function GetThumbnail($img) {
    $img = trim($img);
    if (!empty($img)) return $img;
    else return "images/thumbnail.jpg";
}

function notice($str) {
    echo "<p class='notice'>{$str}</p>";
}

function ViewPages($news_per_page = 10, $mod = "news", $table = "news") {
    global $db;
    $sql = "SELECT count(*) FROM {$table}";
    $data = $db->fetch_array($db->query($sql));
    $total_news = $data[0];
    $total_page = $total_news / $news_per_page;
    if (!is_int($total_page)) {
        settype($total_page, "int");
        $total_page++;
    }
    
    $curr_page = (isset($_GET['page'])) ? $_GET['page'] : 1;
    $prev_page = ($curr_page > 1) ? $curr_page - 1 : 1;
    $next_page = ($curr_page < $total_page ) ? $curr_page + 1 : $total_page;
    
    $link = "index.php?mod={$mod}&act=list&page=";
    
    if ($total_page > 1) {
        echo "\n<div class='view-page'>";
        PageLink('First', $link, 1 );
        PageLink('Previous', $link, $prev_page );
        
        $fist_page =  (($curr_page - 5) > 0) ? $curr_page - 5 : 1 ;
        $last_page =  (($curr_page + 5) < $total_page) ? $curr_page + 5 : $total_page ;
                   
        for ($i=$fist_page; $i<=$last_page; $i++)
        {
            $is_current = ($i == $curr_page) ? 1 : 0;
            PageLink($i, $link, $i, $is_current);
        }
        
        PageLink('Next', $link, $next_page );
        PageLink('Last', $link, $total_page );
        echo "\n</div>";
    } 
}

function PageLink($title, $link, $page_number, $is_current = 0) {
    $link .= $page_number;
    if ($is_current) {
        echo "\n<a class='page-current'>$title</a>";    
    } else {
        echo "\n<a href='$link' class='page-link'>$title</a>";
    }
}

?>